package com.book.system.dao;

import com.book.system.entity.User;

import java.util.List;
import java.util.Map;


public class UserDao {

    /**
     * 登录方法
     * @param user 用户实体类
     * @return list
     */
    public List<Map<String, Object>> login(User user) {
        String sql = "select * from user where username = ? and password = ?";
        List<Map<String, Object>> list = BaseDao.executeQuery(sql, user.getUsername(), user.getPassword());
        return list;
    }

    /**
     * 查询所有
     */
    public List<Map<String, Object>> getList(String name, Integer flag) {

        StringBuilder tempSql = new StringBuilder("select * from user where 1=1 ");

        if (name != null && !"".equals(name)) {
            tempSql.append(" and username like '%").append(name).append("%'");
        }
        if (flag != null && flag != 2) {
            tempSql.append(" and flag = ").append(flag);
        }
        String sql = tempSql.toString();
        List<Map<String, Object>> list = BaseDao.executeQuery(sql);
        return list;
    }

    /**
     * 增加用户
     * @param user
     * @return
     */
    public int addUser(User user) {
        String sql = "insert into user(name,username,password,sex,birth,address,phone,flag)"
                + "value(?,?,?,?,?,?,?,?)";
        return BaseDao.executeUpdate(sql, user.getName(), user.getUsername(), user.getPassword(), user.getSex(),
                user.getBirth(), user.getAddress(), user.getPhone(), user.getFlag());
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    public int delete(Integer id) {
        String sql = "delete from user where id = ?";
        return BaseDao.executeUpdate(sql, id);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    public int updateById(User user) {
        String sql = "update user set name = ?,username = ?,password = ?," +
                "sex = ?,birth = ?,address = ?,phone = ?,flag = ? where id=?";
        return BaseDao.executeUpdate(sql, user.getName(), user.getUsername(),
                user.getPassword(), user.getSex(), user.getBirth(),
                user.getAddress(), user.getPhone(), user.getFlag(), user.getId());

    }
    public int rePasswordById(User user){
        String sql = "update user set password = ? where id = ?";
        return BaseDao.executeUpdate(sql,user.getPassword(),user.getId());
    }
}